﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace NewsInfo
{
   public static class NewsInfoControl
    {
        public static int Insert(NewsInfo b)
        {
            try
            {
                News.Instance.NewsInfo.InsertOnSubmit(b);
                News.Instance.SubmitChanges();
                return b.NewsID;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static void Update(NewsInfo b)
        {
            try
            {
                News.Instance.SubmitChanges(System.Linq.ConflictMode.ContinueOnConflict);
            }
            catch (System.Linq.ChangeConflictException)
            {
                foreach (System.Linq.ObjectChangeConflict cc in News.Instance.ChangeConflicts)
                {
                    cc.Resolve(System.Linq.RefreshMode.KeepChanges); //原先更新有效，冲突字段以当前更新为准
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static NewsInfo Delete(int id)
        {
            try
            {
                NewsInfo b = GetModel(id);
                News.Instance.NewsInfo.DeleteOnSubmit(b);
                News.Instance.SubmitChanges();
                return b;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static NewsInfo GetModel(int id)
        {
            try
            {
                return News.Instance.NewsInfo.FirstOrDefault(b => b.NewsID == id);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static int GetCount(Expression<Func<NewsInfo, bool>> predicate)
        {
            try
            {
                if (predicate == null)
                {
                    return News.Instance.NewsInfo.Count();
                }
                else
                {
                    return News.Instance.NewsInfo.Where(predicate).Count();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static List<NewsInfo> GetList(int startIndex, int pageSize, Expression<Func<NewsInfo, bool>> predicate)
        {
            try
            {
                if (predicate == null)
                {
                    return News.Instance.NewsInfo.OrderByDescending(b => b.NewsID).Skip(startIndex - 1).Take(pageSize).ToList();
                }
                else
                {
                    return News.Instance.NewsInfo.Where(predicate).OrderByDescending(b => b.NewsID).Skip(startIndex - 1).Take(pageSize).ToList();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static List<NewsInfo> GetList(Expression<Func<NewsInfo, bool>> predicate)
        {
            try
            {
                if (predicate == null)
                {
                    return News.Instance.NewsInfo.OrderByDescending(b => b.NewsID).ToList();
                }
                else
                {
                    return News.Instance.NewsInfo.Where(predicate).OrderByDescending(b => b.NewsID).ToList();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

    }
}
